﻿using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;

public class BubbleTipsWin : MonoBase
{
	private BubbleTipsController _controller;

	private GameObject _tipsPrefab;
	private List<GameObject> _itemList;
	private bool _isRunning;
	private Coroutine _coroutine;

	protected override void Awake()
	{
		base.Awake();
		_tipsPrefab = transform.Find("TipsPrefab").gameObject;
		_tipsPrefab.SetActive(false);
		_itemList = new List<GameObject>();
	}

	protected override void Start()
	{
		base.Start();
		UpdateInfo();
	}

	public void UpdateInfo()
	{
		if (_isRunning) return;
		AddItem();
	}

	private void AddItem()
	{
		if (_controller.TipsList.Count > 0)
		{
			_isRunning = true;
			string tips = _controller.TipsList.Dequeue();
			GameObject go = GameTools.AddChild(transform, _tipsPrefab);
			go.SetActive(true);
			go.transform.localPosition = Vector3.up * 120;
			go.AddComponent<BubbleTipsItem>().SetInfo(tips);
			if (_itemList.Count > 0)
			{
				for (int i = _itemList.Count - 1; i >= 0; --i)
				{
					_itemList[i].transform.DOLocalMoveY(44, 0.3f).SetEase(Ease.OutQuad).SetRelative();
				}
			}
			_itemList.Add(go);
			_coroutine = StartCoroutine(OnTime());
		}
		else
		{
			_isRunning = false;
		}
	}

	public void RemoveItem(GameObject go)
	{
		if (_itemList.Contains(go))
		{
			_itemList.Remove(go);
		}
		if (_itemList.Count == 0) { WindowManager.GetInstance().CloseWindow(Window.BubbleTipsWin); }
	}

	private IEnumerator OnTime()
	{
		yield return new WaitForSeconds(0.3f);
		AddItem();
	}

	protected override void OnDestroy()
	{
		base.OnDestroy();
		if (_coroutine != null) StopCoroutine(_coroutine);
	}

	public BubbleTipsController Controller { set { _controller = value; } }
}
